package org.dxy.infrastructure.persistent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.dxy.infrastructure.entity.ParamDefinitionEntity;
import org.dxy.infrastructure.persistent.mapper.ParamDefinitionMapper;
import org.dxy.infrastructure.persistent.service.ParamDefinitionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description: 参数定义Service实现类
 * @author: dxy
 * @createTime: 2025/1/21
 */
@Service
public class ParamDefinitionServiceImpl extends MPJBaseServiceImpl<ParamDefinitionMapper, ParamDefinitionEntity> implements ParamDefinitionService {

    @Override
    public Page<ParamDefinitionEntity> selectPageList(Page<ParamDefinitionEntity> page, ParamDefinitionEntity query) {
        LambdaQueryWrapper<ParamDefinitionEntity> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(query.getCode())) {
            wrapper.like(ParamDefinitionEntity::getCode, query.getCode());
        }
        if (StringUtils.hasText(query.getName())) {
            wrapper.like(ParamDefinitionEntity::getName, query.getName());
        }
        if (StringUtils.hasText(query.getInputType())) {
            wrapper.eq(ParamDefinitionEntity::getInputType, query.getInputType());
        }
        if (StringUtils.hasText(query.getDataType())) {
            wrapper.eq(ParamDefinitionEntity::getDataType, query.getDataType());
        }
        
        wrapper.orderByDesc(ParamDefinitionEntity::getCreateTime);
        
        return this.page(page, wrapper);
    }

    @Override
    public ParamDefinitionEntity selectByCode(String code) {
        LambdaQueryWrapper<ParamDefinitionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParamDefinitionEntity::getCode, code);
        return this.getOne(wrapper);
    }

    @Override
    public List<ParamDefinitionEntity> selectByInputType(String inputType) {
        LambdaQueryWrapper<ParamDefinitionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParamDefinitionEntity::getInputType, inputType);
        wrapper.orderByAsc(ParamDefinitionEntity::getCode);
        return this.list(wrapper);
    }

    @Override
    public int checkCodeExists(String code, Long excludeId) {
        LambdaQueryWrapper<ParamDefinitionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParamDefinitionEntity::getCode, code);
        if (excludeId != null) {
            wrapper.ne(ParamDefinitionEntity::getId, excludeId);
        }
        return Math.toIntExact(this.count(wrapper));
    }

    @Override
    public int checkParamUsage(Long id) {
        // 这里需要查询process_param_requirement表，检查是否有引用
        // 由于跨表查询，这里返回0，实际项目中可以通过SQL查询或者调用其他Service
        // 或者在Application Service层进行检查
        return 0;
    }

    @Override
    public int batchDeleteByIds(List<Long> ids) {
        // 使用逻辑删除
        return Math.toIntExact(this.removeByIds(ids) ? ids.size() : 0);
    }
}